package ucn.dmf83.sc.workshop1.group3.DomainLayer;

import java.util.ArrayList;
import java.util.Iterator;

import ucn.dmf83.sc.workshop1.group3.DomainLayer.Loan.State;

public class Person {
	private int id;
	private String name;
	private String address;
	private int postalCode;
	private String city;
	private String phone;
	
	private ArrayList<Loan> loans;
	
	
	/**
	 * @param id
	 * @param name
	 * @param address
	 * @param postalCode
	 * @param city
	 * @param phone
	 */
	public Person(int id, String name, String address, int postalCode,
			String city, String phone) {
		this.id = id;
		this.name = name;
		this.address = address;
		this.postalCode = postalCode;
		this.city = city;
		this.phone = phone;
		
		loans = new ArrayList<Loan>();
	}
	

	public Person() {
		loans = new ArrayList<Loan>();
	}


	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the address
	 */
	public String getAddress() {
		return address;
	}
	/**
	 * @param address the address to set
	 */
	public void setAddress(String address) {
		this.address = address;
	}
	/**
	 * @return the postalCode
	 */
	public int getPostalCode() {
		return postalCode;
	}
	/**
	 * @param postalCode the postalCode to set
	 */
	public void setPostalCode(int postalCode) {
		this.postalCode = postalCode;
	}
	/**
	 * @return the city
	 */
	public String getCity() {
		return city;
	}
	/**
	 * @param city the city to set
	 */
	public void setCity(String city) {
		this.city = city;
	}
	/**
	 * @return the phone
	 */
	public String getPhone() {
		return phone;
	}
	/**
	 * @param phone the phone to set
	 */
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	
	
	/*
	 * 
	 *  LOAN FUNCTIONS
	 * 
	 */
	
	public void addLoan(Loan loan) {
		this.loans.add(loan);	
	}
	
	public void addLoan(String borrowDate, int period, Copy copy) {
		int id = this.loans.size();
		Loan loan = new Loan(id, borrowDate, period, State.BORROWED, copy);
		
		this.addLoan(loan);
	}
	
	public Loan findLoan(int id)
	{
		Loan foundLoan = new Loan();
		
		for(Iterator<Loan> it = loans.iterator(); it.hasNext();) {
			if(it.next().getId() == id)
				foundLoan = it.next();
		}
		return foundLoan;
	}
	
	public void returnLoan(int loanID)
	{
		Loan loan = this.findLoan(loanID);
		if(loan != null)
			loans.remove(loan);
		else
			System.out.println("The given loan does not exist!");
	}
	
	public ArrayList<Loan> getLoans()
	{
		return this.loans;
	}
}
